package com.myaihui.controller.admin;


import com.myaihui.domain.Account;
import com.myaihui.domain.Market;
import com.myaihui.domain.PropertyMgr;
import com.myaihui.domain.SysUser;
import com.myaihui.foundation.constant.SysUserConstant;
import com.myaihui.foundation.model.Pager;
import com.myaihui.foundation.query.PropertyMgrQuery;
import com.myaihui.service.PropertyMgrService;
import com.myaihui.service.SysUserService;
import com.myaihui.util.LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * *******************************
 * Created by Tim.
 * form com.myaihui
 * on 2018/6/2
 * *******************************
 */
@Controller
@RequestMapping(value = "/propertyMgr")
public class PropertyMgrController {

    @Autowired
    private PropertyMgrService propertyMgrService;

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping(value = "/")
    public ModelAndView home(){
        ModelAndView modelAndView = new ModelAndView("propertyMgr/home");
        return modelAndView;
    }

    @RequestMapping(value = "/page")
    @ResponseBody
    public Pager<PropertyMgr> page(PropertyMgrQuery query){
        return propertyMgrService.getPropertyMgrPage(query);
    }

    @RequestMapping(value = "/add")
    public ModelAndView add(){
        return new ModelAndView("propertyMgr/add");
    }

    @RequestMapping(value = "/{id}")
    public ModelAndView detail(@PathVariable String id){
        ModelAndView modelAndView = new ModelAndView("propertyMgr/edit");
        PropertyMgr propertyMgr = propertyMgrService.get(id);
        SysUser sysUser = sysUserService.findByTypeAndPlatformId(SysUserConstant.PM_ADMIN, id);
        modelAndView.addObject("propertyMgr", propertyMgr);
        modelAndView.addObject("sysUser", sysUser);
        return modelAndView;
    }

    @RequestMapping(value = "/modify")
    public ModelAndView modify(PropertyMgr propertyMgr, Account account, String username, String password){
        propertyMgrService.modify(propertyMgr, account, username, password, LoginUtil.loginUser().getId());
        return new ModelAndView("redirect:/propertyMgr/");
    }
}